﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

namespace asoptions
{
    class ASInterface
    {
        // adapted from:
        // http://boycook.wordpress.com/2008/07/29/c-win32-messaging-with-sendmessage-and-wm_copydata/
        [DllImport("User32.dll")]
        private static extern int RegisterWindowMessage(string lpstring);

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern Int32 FindWindow(String lpClassName, String lpWindowName);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);

        [DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(int hWnd);

        public const int WM_COPYDATA = 0x4A;

        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cbData;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }

        public int sendWindowsStringMessage(int wParam, string msg)
        {
            int result = 0;

            if (hwnd > 0)
            {
                byte[] sarr = System.Text.Encoding.Default.GetBytes(msg);
                int len = sarr.Length;
                COPYDATASTRUCT cds;
                cds.dwData = (IntPtr)100;
                cds.lpData = msg;
                cds.cbData = len + 1;
                result = SendMessage(hwnd, WM_COPYDATA, wParam, ref cds);
                window.addToLog(msg, MessageType.ToAS);
            }

            return result;
        }


        private MainWindow window;
        public int hwnd { get; set; }
        public Characters currentChar { get; set; }
        public static CurrentState state { get; set; }

        public ASInterface(MainWindow win)
        {
            window = win;
            hwnd = 0;
        }

        public bool isRunning()
        {
            if (FindWindow(null, "Audiosurf") > 0)
                return true;
            else
                return false;
        }

        public bool connect(string title)
        {
            hwnd = FindWindow(null, "Audiosurf");
            if (hwnd < 1)
                return false;

            sendWindowsStringMessage(0, "ascommand registerlistenerwindow " + title);

            return true;
        }

        public bool connectFast(string title)
        {
            hwnd = FindWindow(null, "Audiosurf");
            if (hwnd < 1)
                return false;

            sendWindowsStringMessage(0, "ascommand quickstartregisterwindow " + title);
            sendWindowsStringMessage(0, "ascommand registerlistenerwindow " + title);

            return true;
        }

        /// <summary>
        /// Send a command to play a song with the current character.
        /// </summary>
        /// <param name="path">Path to the song to play</param>
        public void playSong(string path)
        {
            sendWindowsStringMessage(0, "ascommand playsong" + Enum.GetName(typeof(Characters), currentChar) + " " + path);
        }

        public void playSong(string path, Characters ch)
        {
            sendWindowsStringMessage(0, "ascommand playsong" + Enum.GetName(typeof(Characters), ch) + " " + path);
        }

        public void playSong(Song s)
        {
            sendWindowsStringMessage(0, "ascommand playsong" + Enum.GetName(typeof(Characters), s.aschar) + " " + s.fullPath);
        }
    }
}
